/*
 * @Author: heinan
 * @Date: 2023-07-23 22:50:57
 * @Last Modified by: heinan
 * @Last Modified time: 2023-08-01 21:52:43
 */
"use strict";
const { Controller } = require("egg");
/**
 *  @Controller 直播管理
 */
class Live extends Controller {
  /**
   * @Summary 获取直播数据列表
   * @Description 获取直播数据列表
   * @Router GET /live
   * @Request query string IdName 名称
   * @Request query Number status 直播状态
   * @Request query Number pageCount 页码 默认 1
   * @Request query Number pageSize 单页数量 默认 10
   */
  async live() {
    const { ctx } = this;
    let {
      IdName = "",
      status = "",
      pageSize = "10",
      pageCount = "1",
    } = ctx.query;
    if (status == "2") {
      status = "";
    } 
    const live = await this.app.mysql.query(`SELECT * FROM live`);
    const data = await this.app.mysql.query(
      `SELECT * FROM live WHERE name LIKE '%${IdName}%' and status LIKE '%${status}%' limit ${(pageCount - 1) * pageSize
      },${pageSize}`
    );
    ctx.body = {
      code: 200,
      data,
      total: live.length,
    };
  }
  /**
   * @Summary 删除接口
   * @Description 根据直播id删除数据
   * @Router DELETE /delLive
   * @Request query string id 直播数据ID
   */
  async delLive() {
    const { ctx } = this;
    let { id } = ctx.query;
    const data = await this.app.mysql.delete("live", {
      id,
    });
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "删除成功",
      };
    }
  }
  /**
   * @Summary 更新接口权限
   * @Description 根据身份identity_id,接口更新字段
   * @Router POST /updateLive
   * @Request body liveUpdateRequest *body
   */
  async updateLive() {
    const { ctx } = this;
    let { id, status } = ctx.request.body;
    let sql = "update live set status=? where id=?";
    const data = await this.app.mysql.query(sql, [status, id]);
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "直播状态修改成功",
      };
    }
  }
}

module.exports = Live;
